<?php
require_once("../model/application.php");

$obj_app = new Applications();

if(isset($_POST['submit'])) // neu co su kien click upload ben phia admin
{
    $app_name = $_POST['app_name'];
    $app_status = $_POST['app_status'];
    $description = $_POST['description']; // mo ta chuc nang cua ung dung
    
    
    // viec xu ly du lieu file upload
    $file_name  = $_FILES['file_upload']['name']; // ten file upload
    $file_type  = $_FILES['file_upload']['type']; // kieu du lieu
    $file_size  = $_FILES['file_upload']['size']; // kich co upload
    $file_path  = $_FILES['file_upload']['tmp_name']; // thu muc upload
    $file_error = $_FILES['file_upload']['error']; // loi sinh ra trong qua trinh upload
    
    echo "File name: ". $file_name."<br>";
    echo "File Type: ".$file_type."<br>";
    echo "File size: ".$file_size."<br>"; 
    
    if($file_error  > 0)  {
        echo "File error";
        exit();
    }
    
    // continue xu ly den phan mo rong cua file
    $allowedExts = array("rar", "zip", "7z", "exe");
    $temp = explode(".", $_FILES["file_upload"]["name"]);
    $extension = end($temp); // chen extension vao cuoi ten file
    
    // var_dump($allowedExts); exit();
    // echo 1; exit()
    if($file_type == "application/rar" || $file_type == "application/zip" || $file_type == "application/7z" || $file_type == "image/gif" && in_array($extension, $allowedExts))
    {    
        if(file_exists("../store/".$file_name)){
            echo "File đã tồn tại";
        }
        else
        {
            if($obj_app->insert_application_into_table($app_name, $app_status, $description, $file_name) != "") // chuoi tra ve khac rong
            {
                echo "Success";
                move_uploaded_file($file_path, "../store/".$file_name);
            }
        }
    }
}
